﻿var ppID = "";

//delay function and timer taken from: http://hartzer.wordpress.com/2009/09/18/onkeyup-delay-in-javascript/

function delayTimer() {
    var timer;
    return function (fun, time) {
        clearTimeout(timer);
        timer = setTimeout(fun, time);
    };
}
var delayFunction = delayTimer();


$(document).ready(function () {

    $("#submitDialogButton").click(function () {
        ppID = $('#paypalIDField').val();
    });

    $("#submitAction").click(function () {
        //If they entered a paypal ID
        if (ppID != "") {
            //Pay the person for submitting the survey
            $.ajax({
                type: 'POST',
                async: false,
                url: '/Survey/Pay/',
                success: function (data, textStatus, xhr) {
                    if (data == "Created") {
                        alert("Success: Payment made to PayPal ID: " + ppID);
                    }
                },
                error: function () {
                    alert("We have bigger problems");
                }

            });
        }

        //You could also do other things in here like check to make sure all
        //the required questions are answered, etc
        window.location = "../../Home/Index";
    });

});

function showModal() {
    //If the survey is monetized...
    $('#getPPIDModal').modal('show');
}

function saveTextResponse(questionId, sessionId) {
    var response = document.getElementById("responseText " + questionId).value;
    if (response != null) {
        $.ajax({
            type: 'POST',
            url: '/Survey/SaveResponse/',
            data: { id: questionId, newResponse: response, sessionId: sessionId },
            success: function () {
                // do something
            }
        });
    }
}

function saveRadioResponse(questionId, sessionId, responseNumber) {
    var radio = document.getElementById("responseChoice " + questionId + " " + responseNumber);
    var response;
    if (radio != null) {
        if (!(radio.checked)) { //its not currently checked, which means its about to be (it gets checked as soon as this function ends)
            response = radio.title;
        }
    }
    if (response != null) {
        $.ajax({
            type: 'POST',
            url: '/Survey/SaveResponse/',
            data: { id: questionId, newResponse: response, sessionId: sessionId },
            success: function () {
                // do something
            }
        });
    }
}


function payRequest() {
    //PayPal API Credentials
    var API_UserName = "joseph_1353912988_biz_api1.marquette.edu"; //TODO
    var API_Password = "1353913008"; //TODO
    var API_Signature = "AktegjYw6s7YAs1wFI8i7WmT6OPEA84-mK488CCR-eqlt3gJ1lGxUl.x"; //TODO
    var API_AppID = "APP-80W284485P519543T"; //Default App ID for Sandbox	
    var API_RequestFormat = "JSON";
    var API_ResponseFormat = "JSON";

    //Initialize the pay request values
    var actionType = "PAY";
    var receiverEmail = "joseph_1353913056_per@marquette.edu";
    var receiverAmount = "5.00";
    var currencyCode = "USD";
    var feesPayer = "SENDER";
    var cancelUrl = "~/Home/Index";
    var returnUrl = "~/Home/Index";
    var errorLanguage = "en_US";
    var senderEmail = "joseph_1353912988_biz@marquette.edu";

    //create data packet to send
    var data = { "actionType": actionType,
        "cancelUrl": cancelUrl,
        "currencyCode": currencyCode,
        "feesPayer": feesPayer,
        "receiverList": { "receiver": [{ "email": receiverEmail, "amount": receiverAmount}] },
        "requestEnvelope": [{ "detailLevel": "ReturnAll", "errorLanguage": errorLanguage}],
        "returnUrl": returnUrl,
        "senderEmail": senderEmail
    }

    //convert data object to JSON string
    var jsonData = JSON.stringify(data);

    //url
    var url = "https://svcs.sandbox.paypal.com/AdaptivePayments/Pay";

    // create http request
    var request = new XMLHttpRequest();

    //open the pay url
    request.open("POST", url, true);

    //set the header for the http request
    request.setRequestHeader("X-PAYPAL-REQUEST-DATA-FORMAT", API_RequestFormat);
    request.setRequestHeader("X-PAYPAL-RESPONSE-DATA-FORMAT", API_ResponseFormat);
    request.setRequestHeader("X-PAYPAL-SECURITY-USERID", API_UserName);
    request.setRequestHeader("X-PAYPAL-SECURITY-PASSWORD", API_Password);
    request.setRequestHeader("X-PAYPAL-SECURITY-SIGNATURE", API_Signature);
    request.setRequestHeader("X-PAYPAL-APPLICATION-ID", API_AppID);

    //send the http request
    request.send(jsonData);
}


